package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.mapper.ApFriendMapper;
import com.zhentao.model.common.app.dto.LikeDto;
import com.zhentao.model.common.app.pojo.Friend;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.app.pojo.Likes;
import com.zhentao.model.common.enums.AppHttpCodeEnum;
import com.zhentao.service.ApLikesService;
import com.zhentao.mapper.ApLikesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author 薄超轩
* @description 针对表【likes】的数据库操作Service实现
* @createDate 2024-10-09 09:13:00
*/
@Service
public class ApLikesServiceImpl extends ServiceImpl<ApLikesMapper, Likes>
    implements ApLikesService {
@Autowired
private ApLikesMapper apLikesMapper;
@Autowired
private ApFriendMapper apFriendMapper;
    /**
     * app端：点赞操作
     * @param dto
     * @return
     */
    @Override
    public ResponseResult thumbs(LikeDto dto) {
        if (dto!=null){
            //判单这个用户数是由已经点赞这个帖子
            QueryWrapper<Likes>wrapper=new QueryWrapper<>();
            wrapper.lambda().eq(dto.getUserId()!=null, Likes::getUserId, dto.getUserId());
            wrapper.lambda().eq(dto.getFriendId()!=null, Likes::getFriendId, dto.getFriendId());
            Likes likes = apLikesMapper.selectOne(wrapper);
            if (likes==null){
                //如果数据库里面没有,就执行添加操作
                Likes likes1=new Likes();
                likes1.setFriendId(dto.getFriendId());
                likes1.setUserId(dto.getUserId());
                likes1.setTime(new Date());
                apLikesMapper.insert(likes1);
                //点赞完成之后，还需要修改交友圈的点赞数量
                Friend friend = apFriendMapper.selectById(dto.getFriendId());
                System.out.println(friend);
                friend.setLikes(friend.getLikes()+1);
                apFriendMapper.updateById(friend);
                return ResponseResult.okResult("点赞成功");
            } else if (likes!=null) {
                //如果这个用户给这个作品点赞过，咱们就需要删除这个点赞表
                apLikesMapper.delete(wrapper);
                //删除完成之后，还需要修交友圈帖子点赞量
                Friend friend = apFriendMapper.selectById(dto.getFriendId());
                friend.setLikes(friend.getLikes()-1);
                apFriendMapper.updateById(friend);
                return ResponseResult.okResult("你已经取消点赞");
            }
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }
}




